/**
 * 
 */
package com.xbang.attendance.schedule;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.xbang.attendance.entity.product.Product;
import com.xbang.attendance.entity.stock.Inventory;
import com.xbang.attendance.entity.stock.InventoryWarningRecord;
import com.xbang.attendance.entity.system.EntityWarning;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.entity.system.WarehouseProductAgreement;
import com.xbang.attendance.service.InventoryService;
import com.xbang.core.schedule.ScheduleProcess;

import enumeration.WarningType;

/**
 * @author dev.zhang
 * 
 */
// @Service("warningScheduleProccess")
public class WarningScheduleProcess implements ScheduleProcess {

	// @Autowired
	// @Qualifier("baseService")
	private InventoryService inventoryService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.xbang.core.schedule.ScheduleProcess#process()
	 */
	public void process() throws Exception {

		String hqlSentence = "FROM EntityWarning entitywarning WHERE 1 = 1 ";

		List<EntityWarning> entityWarnings = inventoryService.searchForList(hqlSentence, new HashMap());
		Set<Inventory> warningInventorys = new HashSet<Inventory>();

		for (EntityWarning entityWarning : entityWarnings) {
			Warehouse warehouse = entityWarning.getWarehouse();
			Product product = entityWarning.getProduct();

			hqlSentence = "From Inventory inventory WHERE inventory.warehouse.id = :warehouseId and inventory.product.id = :productId ";
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("warehouseId", warehouse.getId());
			parameters.put("productId", product.getId());

			List<Inventory> inventorys = inventoryService.searchForList(hqlSentence, parameters);

			for (Inventory inventory : inventorys) {
				// //
				if (entityWarning.getWarningType().compareTo(WarningType.constants) == 0) {
					// 常量
					BigDecimal constantsAmount = entityWarning.getNumber();
					if (entityWarning.entityWarningEqules(constantsAmount, entityWarning.getWarningEqualesType(), inventory.getAmount())) {
						// /增加预警信息

						InventoryWarningRecord inventoryWarningRecord = new InventoryWarningRecord().initInventoryWarningRecord(entityWarning, warehouse,
								product, inventory);

						inventoryWarningRecord.setInventory(inventory);
						inventoryService.save(inventoryWarningRecord);
						inventory.setInventoryWarningRecord(inventoryWarningRecord);
						inventoryService.update(inventory);
						warningInventorys.add(inventory);
					}
				} else {
					// 百分比
					BigDecimal constantsAmount = entityWarning.getNumber();
					BigDecimal percentage = entityWarning.getPercentage();
					if (entityWarning.entityWarningEqules(constantsAmount, percentage, entityWarning.getWarningEqualesType(), inventory.getAmount())) {
						// /增加预警信息
						InventoryWarningRecord inventoryWarningRecord = new InventoryWarningRecord().initInventoryWarningRecord(entityWarning, warehouse,
								product, inventory);
						inventoryService.save(inventoryWarningRecord);
						inventory.setInventoryWarningRecord(inventoryWarningRecord);
						inventoryService.update(inventory);
						warningInventorys.add(inventory);
					}
				}

			}

		}

		inventoryService.updateInventoryWarning(warningInventorys);
		updateWarehouseProductAdg();
	}

	/**
	 * 根据产品约定数量跟新预警信息
	 * 
	 * @throws Exception
	 */
	public void updateWarehouseProductAdg() throws Exception {
		Set<Inventory> warningInventorys = new HashSet<Inventory>();
		List<Inventory> inventorys = inventoryService.getObjects(Inventory.class);
		List<WarehouseProductAgreement> warehouseProductAdreements = inventoryService.getObjects(WarehouseProductAgreement.class);
		if (null != inventorys && !inventorys.isEmpty()) {
			for (Inventory inventory : inventorys) {
				for (WarehouseProductAgreement warehouseProductAgreement : warehouseProductAdreements) {
					if (inventory.getProduct().getId().equals(warehouseProductAgreement.getProduct().getId())) {
						if (inventory.getWarehouse().getId().equals(warehouseProductAgreement.getWarehouse().getId())) {
							BigDecimal inventoryProductNumber = inventory.getAmount() == null ? new BigDecimal(0) : inventory.getAmount();
							BigDecimal addreementNumber = warehouseProductAgreement.getNumber() == null ? new BigDecimal(0) : warehouseProductAgreement
									.getNumber();
							if (inventoryProductNumber.compareTo(addreementNumber.multiply(new BigDecimal(0.5))) < 0) {
								InventoryWarningRecord inventoryWarningRecord = new InventoryWarningRecord().initInventoryWarningRecord(
										warehouseProductAgreement, inventory);
								inventoryWarningRecord.setInventory(inventory);
								inventoryService.save(inventoryWarningRecord);
								inventory.setInventoryWarningRecord(inventoryWarningRecord);
								//inventory.setAgreementAmount(addreementNumber);
								inventoryService.update(inventory);
								warningInventorys.add(inventory);
							}
						}
					}
				}
			}
		}
		inventoryService.updateInventoryWarning(warningInventorys);
	}

	/**
	 * @return the inventoryService
	 */
	public InventoryService getInventoryService() {
		return inventoryService;
	}

	/**
	 * @param inventoryService
	 *            the inventoryService to set
	 */
	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

}
